package frobagroup.controller.frontEnd;

import frobagroup.entities.common.Credential;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.services.frontEnd.SettingsService;
import frobagroup.utils.Constants;
import frobagroup.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Tinsley on 2018/5/10.
 */
@RequestMapping("/webAPI/setting")
@RestController
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @RequestMapping(value = "/findProfileById", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> findProfileById(HttpServletRequest request
    ) throws ServletException {
        return ResponseModel.success(settingsService.findProfileById((String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/updateProfile", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> updateProfile(@RequestBody UserProfile userProfile, HttpServletRequest request
    ) throws ServletException {
        return ResponseModel.success(settingsService.updateProfile(userProfile,(String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/changePortrait", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> changePortrait(@RequestParam(value = "myPhoto", required = false) MultipartFile myPhoto,
                                                                HttpServletRequest request
    ) throws Exception {
        return ResponseModel.success(settingsService.changePortrait(myPhoto,(String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> updateUserPassword(@RequestBody Credential credential, HttpServletRequest request
    ) throws Exception {
        return ResponseModel.success(settingsService.updateUserPassword(credential,(String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/findFollowProducts", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> findFollowProducts(HttpServletRequest request) throws Exception{
        return ResponseModel.success(settingsService.findFollowProducts((String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

}
